/*
* I2C
* Author : Vo Tuan Anh
* Email  : votuananhs@gmail.com
*/
#include "stdint.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"

#ifndef _I2C_H_
#define _I2C_H_

#ifdef __cplusplus
 extern "C" {
#endif /* __cplusplus */


/* device address */
#define I2C_SLAVE_ADDRESS7      0xA0   

/* speed i2c */
#define I2C_SPEED               100000   
   
#define TIMEOUT                 ((uint32_t)0x0000FFFF)
   
#define I2C_SDA_GPIO_PORT       GPIOB
#define I2C_SCL_GPIO_PORT       GPIOB
   
#define I2C_SDA_PIN             ((uint16_t)GPIO_Pin_7)
#define I2C_SCL_PIN             ((uint16_t)GPIO_Pin_6)
   
#define I2C_SDA_SOURCE          ((uint16_t)GPIO_PinSource7)
#define I2C_SCL_SOURCE          ((uint16_t)GPIO_PinSource6)
   
#define I2C_SDA_AF              GPIO_AF_I2C1
#define I2C_SCL_AF              GPIO_AF_I2C1
   
#define I2C_CLK                 RCC_APB1Periph_I2C1
#define I2C_SDA_GPIO_CLK        RCC_AHB1Periph_GPIOB
#define I2C_SCL_GPIO_CLK        RCC_AHB1Periph_GPIOB
   
typedef struct 
{
  uint8_t DataLength_I2C;
  uint8_t *Data_I2C;
  uint8_t DeviceAddress_I2C;
  uint8_t DataAddress_I2C;
} ReqArrayData_I2C;

typedef enum
{
  i2c_ok = 0,
  i2c_busy,
  i2c_startError,
  i2c_addressSendFault,
  i2c_dataSubaddressFault,
  i2c_dataSendFault,
  i2c_repeatedStartFault,
  i2c_dataReadFault,
  i2c_repeatedAddressFault,
  i2c_stopReadFault
} Status_I2C;

Status_I2C InitLowLevel_I2C(I2C_TypeDef* I2Cx);
Status_I2C WritePolling_I2C(I2C_TypeDef* I2Cx, const ReqArrayData_I2C *ReqData);
Status_I2C ReadPolling_I2C(I2C_TypeDef* I2Cx, ReqArrayData_I2C *ReqData);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _I2C_H_ */
